package com.baike.filter;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
/**
 * Zuul 请求过滤配置
 * 我们请求服务，也可以来设置身份验证，也就是过滤非法请求；Zuul通过ZuulFilter过滤器实现；
 * @author John
 *
 */
public class AccessFilter extends ZuulFilter{

	Logger logger=Logger.getLogger(AccessFilter.class);
	/**
	 * 过滤器的具体执行逻辑
	 */
	@Override
	public Object run() throws ZuulException {
		// TODO Auto-generated method stub
		RequestContext conx = RequestContext.getCurrentContext();
		HttpServletRequest request = conx.getRequest();
		String parameter = request.getParameter("accessToken");
		logger.info(request.getRequestURL().toString()+"请求访问");
		if(parameter==null){
			logger.error("accessToken为空");
			conx.setSendZuulResponse(false);
			conx.setResponseStatusCode(401);
			conx.setResponseBody("{\"result\":\"accessToken is empty!\"}");
			return null;
		}
		logger.info(request.getRequestURL().toString()+"请求成功");
		return null;
	}

	/**
	 * 判断该过滤器是否要被执行
	 */
	@Override
	public boolean shouldFilter() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 过滤器的执行顺序
	 */
	@Override
	public int filterOrder() {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 过滤器的类型 这里用pre，代表会再请求被路由之前执行
	 */
	@Override
	public String filterType() {
		// TODO Auto-generated method stub
		return null;
	}

}
